<?php

namespace App\Http\Controllers\Api;

use App\Events\EnterCar;
use App\Events\LeaveCar;
use App\Models\FlowParkRecord;
use App\Models\ParkData;
use App\Models\ParkChannels;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use App\Models\ParkRecord;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;


class EnterAndLeaveCarController extends AppBaseController
{


    public function index(Request $request)
    {
        $type = $request->get('status');
        $data = json_decode($request->get('data'));

        Log::info('ENTER_LEAVE_CAR: '. $request->get('data'));
        
        $parkRecord = new ParkRecord();

        $table = 't_park_record_' . $data->park_code;

        if (!\Schema::connection('park_cloud')->hasTable($table)) {
            return \Response::json(['message' => '车场记录表未找到']);
        }

        $parkRecord->setTable($table);


        $record = \DB::connection('park_cloud')->table($table)->where('record_id', $data->record_id)->first();

        if (!$record) {
            \Log::error('未找到停车到记录.', ['data' => $data]);
            return \Response::json(['message' => '车场记录未找到']);
        }

        $etc_num = ParkChannels::where("name",'like',"'ETC%'")
            ->where("park_code",$record->park_code)
            ->where("arm_code",($type == 1?$record->in_arm_code:$record->out_arm_code))
            ->count();
        if(!empty($etc_num)){
            $record->etc_flag = 1;
        } else {
            $record->etc_flag = 0;
        }
        $fpr = FlowParkRecord::where(['park_code'=>$data->park_code,'record_id'=>$data->record_id,'type'=>$type])->count();
        if($fpr){
            return \Response::json(['message' => '车场记录已经统计过']);
        }
        FlowParkRecord::insert(['park_code'=>$data->park_code,'record_id'=>$data->record_id,'type'=>$type]);
        //进车
        if ($type == 1) event(new EnterCar($record));

        //出车
        if ($type == 2) {
            $record->charge_items = json_decode($record->charge_items);
            event(new LeaveCar($record));
        }

        return \Response::json(['message' => 'ok']);
    }

    public function test(Request $request)
    {
        $type = $request->get('status',2);

        $parkRecord = new ParkRecord();

        $table = 't_park_record_7000000114';

        if (!\Schema::connection('park_cloud')->hasTable($table)) {
            return \Response::json(['message' => '车场记录表未找到']);
        }

        $parkRecord->setTable($table);

        $record = \DB::connection('park_cloud')->table($table)->where('record_id', 6)->first();
        if (!$record) {
            return \Response::json(['message' => '车场记录未找到']);
        }
        $fpr = FlowParkRecord::where(['park_code'=>'7000000114','record_id'=>$record->record_id])->count();
        if($fpr){
//            return \Response::json(['message' => '车场记录已经统计过']);
        }
        FlowParkRecord::insert(['park_code'=>'7000000114','record_id'=>$record->record_id]);
        $etc_num = ParkChannels::where("park_code",$record->park_code)
            ->where("arm_code",($type == 1?$record->in_arm_code:$record->out_arm_code))
            ->count();
        if(!empty($etc_num)){
            $record->etc_flag = 1;
        } else {
            $record->etc_flag = 0;
        }
        //进车
        if ($type == 1) event(new EnterCar($record));
        //出车
        if ($type == 2) {
            $record->charge_items = json_decode($record->charge_items);
            event(new LeaveCar($record));
        }

        return \Response::json(['message' => 'ok',"type"=>$type]);
    }


}
